package com.night.icm.service.base.mapper;

import com.night.icm.model.base.StudentDTO;
import com.night.icm.persistence.base.entity.Student;
import org.mapstruct.*;

import java.util.List;

/**
 * StudentMapper
 *
 * @author Liao ZhiYong
 * @date 2020/1/29
 */
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface StudentMapper {

    /**
     * DTO转实体
     *
     * @param studentDTO DTO
     * @return 实体
     */
    Student toEntity(StudentDTO studentDTO);

    /**
     * 实体转DTO--供findById使用
     *
     * @param student 实体
     * @return DTO
     */
    @Mapping(source = "academy.id", target = "academyId")
    @Mapping(source = "academy.name", target = "academyName")
    @Mapping(source = "profession.id", target = "professionId")
    @Mapping(source = "profession.name", target = "professionName")
    @Mapping(source = "AClass.id", target = "classId")
    @Mapping(source = "AClass.name", target = "className")
    StudentDTO toDTO(Student student);

    /**
     * 实体转DTO--供分页查询使用
     *
     * @param save 实体
     * @return DTO
     */
    @Named("toDtoByPage")
    @Mapping(target = "studentDetail", ignore = true)
    @Mapping(source = "academy.id", target = "academyId")
    @Mapping(source = "academy.name", target = "academyName")
    @Mapping(source = "profession.id", target = "professionId")
    @Mapping(source = "profession.name", target = "professionName")
    @Mapping(source = "AClass.id", target = "classId")
    @Mapping(source = "AClass.name", target = "className")
    StudentDTO toDtoByPage(Student save);

    /**
     * 实体集转为DTO集
     *
     * @param students 实体集
     * @return DTO集
     */
    @IterableMapping(qualifiedByName = "toDtoByPage")
    List<StudentDTO> toList(List<Student> students);

    /**
     * 更新一个已经存在的实体数据
     *
     * @param studentDTO 存在的DTO
     * @param student    更新后的实体
     */
    void updateEntity(StudentDTO studentDTO, @MappingTarget Student student);
}
